package com.Blog.www.filter;

import com.Blog.www.constant.DataConstant;
import com.Blog.www.constant.PageConstant;
import com.Blog.www.constant.SqlConstant;
import com.Blog.www.constant.TipsConstant;
import com.Blog.www.po.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 管理员功能校验程序
 *
 * @author 君未洋
 */
@WebFilter("/*")
public class ManagerLoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        String contextPath = req.getContextPath();
        String uri = req.getRequestURI();
        if (uri.contains("/ManagerHome.jsp") || uri.contains("/MangerUpdate.jsp") || uri.contains("/ExamineBlog.jsp")
                || uri.contains("/columnList.jsp") || uri.contains("/UserBlog/managerUpdateBlog") || uri.contains("/UserBlog/managerUpdateList") ||
                uri.contains("/UserBlog/examineBlog") || uri.contains("/UserBlog/examineList") ||
                uri.contains("/User/noBanUser") || uri.contains("/User/noBanUserList") || uri.contains("/User/banUser") || uri.contains("/User/banUserList") ||
                uri.contains("/User/managerLogin") || uri.contains("/Column/deleteColumn") || uri.contains("/Column/deleteColumnList")) {
            //上面一大排就是管理员操作时会使用到的了
            //先获取上传到的user数据
            User user = (User) req.getSession().getAttribute(DataConstant.USER_A);
            if (user == null) {
                req.setAttribute(PageConstant.USER_C, TipsConstant.USER_LOGIN_A);
                req.getRequestDispatcher("/login.jsp").forward(servletRequest, servletResponse);
                return;
            } else if (!(SqlConstant.IDENTITY_C.equals(user.getIdentity()) || SqlConstant.IDENTITY_D.equals(user.getIdentity()))) {
                req.setAttribute(PageConstant.USER_D, TipsConstant.USER_LOGIN_F);
                req.getRequestDispatcher("/UserBlog/findBlogByPage").forward(req, servletResponse);
                return;
            }
            //前面的情况都没问题了,就可以放行了
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            //其他情况直接放行
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
